<?php


namespace app\api\controller;


use app\api\validate\User;
use think\exception\ValidateException;

class Login extends ApiBase
{
    public function index()
    {
        if (!request()->isPost()){
            return show(2,'非法请求');
        }


        $phone_number = request()->param('phone_number', '', 'trim');
        $code = input('param.code', 0, 'intval');
        $type = input('param.type', 0, 'intval');

        $data = [
            'phone_number' => $phone_number,
            'code' => $code,
            'type' => $type
        ];

        try {
            validate(User::class)->scene('login')->check($data);
        } catch (ValidateException $e) {
            return show(2, $e->getMessage());
        }

        $res = (new \app\common\business\User())->login($data);
        if (!$res) {
            return show(2, '登录失败');
        }
        return show(1, '登录成功',$res);
    }

}